// pages/my/indexPage/index.js
var App = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        swiper: ["测试评估", "训练计划"],
        active: 0,
        nums: 0,
        bnums: 0,
        test: [],
        api: App.api,
        page: 2,
        bb: 2,
        pages: []
    },
    //点击切换
    tab: function(e) {
        var that = this;
        var active = e.target.dataset.index;
        that.setData({
            active: active
        })
        if (active == 1) {
            that.getplan(1);
        } else if (active == 0) {
            that.getTest(1);
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.getTest(1);
    },
    //得到测试题
    getTest: function(num) {
        var that = this;
        App.ajax.ajax('testUserSchedule.do?method=testList', {
            JSESSIONID: wx.getStorageSync('JSESSIONID'),
            page: num,
            limit: 4
        }, function(res) {
            //    console.log(res.data.)
            if (res.data.jsonType == "success") {
                for (var i = 0; i < res.data.items.length; i++) {
					if (res.data.items[i].money != "免费" && res.data.items[i].money != "已付款") {
                        res.data.items[i].money = "￥" + res.data.items[i].money + "元/周";
                    }

                }
                var nums = Math.ceil(res.data.totalCount / 3);
                console.log(nums)
                that.setData({
                    page: 2,
                    bb: 2,
                    test: res.data.items,
                    count: res.data.totalCount,
                    nums: nums
                })
            }
        })
    },
    //得到计划
    getplan: function(num) {
        var that = this;
        App.ajax.ajax('schdule.do?method=list', {
            JSESSIONID: wx.getStorageSync('JSESSIONID'),
            page: num,
            limit: 4
        }, function(res) {
            console.log(res.data)
            if (res.data.jsonType == "success") {
                for (var i = 0; i < res.data.items.length; i++) {
					if (res.data.items[i].money != "免费" && res.data.items[i].money != "已付款") {
						res.data.items[i].money = "￥" + res.data.items[i].money + "元/周";
                    }

                }
                var bnums = Math.ceil(res.data.totalCount / 3);
                that.setData({
                    test: res.data.items,
                    count: res.data.totalCount,
                    bnums: bnums,
                    page: 2,
                    bb: 2
                })
            }
        })
    },
    //获取测试内容并跳转
    enterTest: function(e) {
        var that = this;
        var pay = e.currentTarget.dataset.pay;
        var id = e.currentTarget.dataset.id;
        var alpay = pay.substring(1);
        var rmb = parseFloat(alpay);
        console.log(rmb);
        var ctype = e.currentTarget.dataset.ctype;
        if (that.data.active == 1) {
            if (ctype == "动作") {
                if (rmb > 0) {
                    App.ajax.ajax('payPre.do', {
                        JSESSIONID: wx.getStorageSync("JSESSIONID"),
                        openId: wx.getStorageSync("openid"),
						scheduleType: "general",
                        scheduleId: id
                    }, function(res) {
                        //微信支付
                        res.data.paySign = res.data.sign;
                        console.log(res.data)
                        wx.requestPayment(res.data)
                    })
                } else {
                    wx.navigateTo({
                        url: '../actionDetail/index?id=' + id,
                    })
                }
            } else if (ctype == "跑步") {
                if (rmb > 0) {
                    App.ajax.ajax('payPre.do', {
                        JSESSIONID: wx.getStorageSync("JSESSIONID"),
                        openId: wx.getStorageSync("openid"),
						scheduleType: "general",
                        scheduleId: id
                    }, function(res) {
                        //微信支付
                        res.data.paySign = res.data.sign;
                        console.log(res.data)
                        wx.requestPayment(res.data)
                    })
                } else {
                    wx.navigateTo({
                        url: '../animate/index?id=' + id,
                    })
                }

            }


        }

        //console.log(id);
        // wx.navigateTo({
        // 	url: '../tranPlan/index?id=' + id,
        // })
        // if (pay == "免费") {
        //     if (id == "006") {
        //         wx.navigateTo({
        //             url: '../animate/index?id=' + id,
        //         })
        //     } else {
        //         wx.navigateTo({
        //             url: '../question/index?id=' + id,
        //         })
        //     }
        // } else {
        //     console.log("收费的呢")
        // }
    
    else if (that.data.active == 0) {
        if (rmb > 0) {
            App.ajax.ajax('payPre.do', {
                JSESSIONID: wx.getStorageSync("JSESSIONID"),
                openId: wx.getStorageSync("openid"),
                scheduleType: "test",
                scheduleId: id
            }, function(res) {
                //微信支付
                res.data.paySign = res.data.sign;
                console.log(res.data)
                wx.requestPayment(res.data)
            })


            // wx.showLoading({
            // 	title: '加载中',
            // 	mask:true
            // })


        } else {
            console.log(id);
			var idd = id;
            wx.navigateTo({
				url: '../question/index?id=' + id + '&idd=' + idd,
            })
        }

    }
    // if (pay == "免费") {
    //     wx.navigateTo({
    //         url: '../question/index?id=' + id,
    //     })
    // } else {
    //     console.log("收费的呢")
    // }
    // else if(pay == "关注公众号"){

    // }
},
/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady: function() {

},

/**
 * 生命周期函数--监听页面显示
 */
onShow: function() {

},

/**
 * 生命周期函数--监听页面隐藏
 */
onHide: function() {

},

/**
 * 生命周期函数--监听页面卸载
 */
onUnload: function() {

},

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh: function() {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom: function() {
    console.log(2)
    var that = this;
    var active = that.data.active;
    var page = that.data.page;
    var bb = that.data.bb;
    if (active == 0) {
        if (page > that.data.nums) {
            wx.showToast({
                title: '已加载全部',
                icon: 'none',
            })
        } else {
            that.bpages(that.data.page)
            that.setData({
                page: page + 1
            })
        }
    } else if (active == 1) {
        if (bb > that.data.bnums) {
            wx.showToast({
                title: '已加载全部',
                icon: 'none',
            })
        } else {
            that.pages(that.data.bb)
            that.setData({
                bb: bb + 1
            })
        }
    }
},
//分页-得到训练计划
pages: function(num) {
    var that = this;
    App.ajax.ajax('schdule.do?method=list', {
        JSESSIONID: wx.getStorageSync('JSESSIONID'),
        page: num,
        limit: 3
    }, function(res) {
        if (res.data.jsonType == "success") {
            for (var i = 0; i < res.data.items.length; i++) {
				if (res.data.items[i].money != "免费" && res.data.items[i].money != "已付款") {
					res.data.items[i].money = "￥" + res.data.items[i].money + "元/周";
                }
            }
            var test = that.data.test;
            var allData = test.concat(res.data.items);
            that.setData({
                test: allData
            })
        }
    })
},
//得到测试
bpages: function(num) {
    var that = this;
    App.ajax.ajax('testUserSchedule.do?method=testList', {
        JSESSIONID: wx.getStorageSync('JSESSIONID'),
        page: num,
        limit: 3
    }, function(res) {
        if (res.data.jsonType == "success") {
            for (var i = 0; i < res.data.items.length; i++) {
				if (res.data.items[i].money != "免费" && res.data.items[i].money != "已付款") {
                    res.data.items[i].money = "￥" + res.data.items[i].money + "元/周";
                }
            }
            var test = that.data.test;
            var allData = test.concat(res.data.items);
            that.setData({
                test: allData
            })
        }
    })
},

/**
 * 用户点击右上角分享
 */
onShareAppMessage: function() {

}
})